package com.shali.cloud.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.shali.cloud.entity.User;

@RestController
public class MovieController {

	@Autowired
	private RestTemplate restTemplate;
	
	@Autowired
	private LoadBalancerClient loadBalancerClient;
	
	@GetMapping("/movie/{id}")
	public User findById(@PathVariable Long id) {
		
		ServiceInstance instance = this.loadBalancerClient.choose("microservice-provider-user");
		System.out.println("----::"+instance.getServiceId()+""+instance.getHost()+":"+instance.getPort());
		//VIP(virtual IP) microservice-provider-user
		return this.restTemplate.getForObject("http://microservice-provider-user/findOne/"+id, User.class);
	}
	
	@GetMapping("/test")
	public String test() {
		ServiceInstance instance1 = this.loadBalancerClient.choose("microservice-provider-user");
		System.out.println("---------::"+instance1.getServiceId()+":"+instance1.getHost()+":"+instance1.getPort());
		ServiceInstance instance2 = this.loadBalancerClient.choose("microservice-provider-user1");
		System.out.println("---------::"+instance2.getServiceId()+":"+instance2.getHost()+":"+instance2.getPort());
		return "1";
	}
}
